package com.alkaid.anime.utils;

import com.alkaid.commons.msg.MsgDefinition;
import com.alkaid.commons.ex.CacheException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Kylin
 */
@Component
public class RedisUtils {
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void save2Redis(String key, Object value, long expireTime) throws CacheException {
        if (expireTime == 0) {
            // 如果是0，则代表-1
            expireTime = -1;
        } else if (expireTime < -1) {
            // 如果小于-1，代表的数绝对值
            expireTime = Math.abs(expireTime);
        }
        try {
            redisTemplate.opsForValue().set(key, value);
            if (expireTime > 0) {
                // 设置有效期，时间单位是s
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CacheException(MsgDefinition.CACHE_FAILED);
        }
    }

    public Object getFromRedis(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            return null;
        }
    }

    public boolean deleteKey(String key) {
        try {
            Boolean result = redisTemplate.delete(key);
            return result != null && result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CacheException(MsgDefinition.CACHE_DELETE_FAILED);
        }
    }

    /**
     * list 向右批量插入
     *
     * @param key        the key
     * @param expireTime the expire time
     * @param members    the members
     * @return the long
     */
    public Long lrPushs(String key, long expireTime, Object ... members) {
        if (expireTime == 0) {
            // 如果是0，则代表-1
            expireTime = -1;
        } else if (expireTime < -1) {
            // 如果小于-1，代表的数绝对值
            expireTime = Math.abs(expireTime);
        }
        try {
            Long result = redisTemplate.opsForList().rightPushAll(key, members);
            if (expireTime > 0) {
                // 设置有效期，时间单位是s
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CacheException(MsgDefinition.CACHE_FAILED);
        }
    }

    /**
     * list 向头部插入一条
     *
     * @param key        the key
     * @param expireTime the expire time
     * @param member     the member
     * @return the long
     */
    public Long llPush(String key, long expireTime, Object member) {
        if (expireTime == 0) {
            // 如果是0，则代表-1
            expireTime = -1;
        } else if (expireTime < -1) {
            // 如果小于-1，代表的数绝对值
            expireTime = Math.abs(expireTime);
        }
        try {
            Long result = redisTemplate.opsForList().leftPush(key, member);
            if (expireTime > 0) {
                // 设置有效期，时间单位是s
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CacheException(MsgDefinition.CACHE_FAILED);
        }
    }

    /**
     * 获取 list 列表
     *
     * @param key   the key
     * @param start 起始位置，0表示第一位
     * @param stop  结束为止，-1表示倒数第一位
     * @return the list
     */
    public List<Object> getList(String key, Long start, Long stop) {
        try {
            return redisTemplate.opsForList().range(key, start, stop);
        } catch (Exception e) {
            return null;
        }
    }
}
